package com.njust.diary.filter;


import com.njust.diary.po.User;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebFilter("/*")
public class LoginAccessFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void destroy() {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        HttpServletRequest request = (HttpServletRequest)  servletRequest;
        HttpServletResponse response =(HttpServletResponse)  servletResponse;
        String path=request.getRequestURI();//项目路径/资源路径
        if(path.contains("/mainPage.html")){
            filterChain.doFilter(request,response);
            return;
        }
        if(path.contains("/login.jsp")){
            filterChain.doFilter(request,response);
            return;
        }
        if(path.contains("/regist.jsp")){
            filterChain.doFilter(request,response);
            return;
        }
         if(path.contains("/sources")){
             filterChain.doFilter(request,response);
             return;
         }
        if(path.contains("/user")){
            String actionName=request.getParameter("actionName");
            if("login".equals(actionName)){
                filterChain.doFilter(request,response);
                return;
            }else if("checkName".equals(actionName)){
                filterChain.doFilter(request,response);
                return;
            }else if("regist".equals(actionName)){
                filterChain.doFilter(request,response);
                return;
        }
        }
        User user = (User) request.getSession().getAttribute("user");
        if(user!=null){
            filterChain.doFilter(request,response);
            return;
        }
        response.sendRedirect("mainPage.html");


    }
}
